package com.raowb.common.util;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 
* @Description:阿拉伯数字转换为中文大写数字
* @author:raowenbiao
* @date 2019年7月1日 下午5:37:29
 */
public class ChineseNumber {

	private static final String[] BEFORE_SCALE = { "万", "仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "" };

	private static final String[] AFTER_SCALE = { "角", "分" };

	private static final String DEFAULT_PATH_SEPARATOR = "\\.";

	private static final Map<String, String> NUMBER_MAPPING = new HashMap<String, String>();
	static {
		NUMBER_MAPPING.put("0", "零");
		NUMBER_MAPPING.put("1", "壹");
		NUMBER_MAPPING.put("2", "贰");
		NUMBER_MAPPING.put("3", "叁");
		NUMBER_MAPPING.put("4", "肆");
		NUMBER_MAPPING.put("5", "伍");
		NUMBER_MAPPING.put("6", "陆");
		NUMBER_MAPPING.put("7", "柒");
		NUMBER_MAPPING.put("8", "捌");
		NUMBER_MAPPING.put("9", "玖");
	}

	public static String getChineseNumber(String number) {
		return getChineseNumber(number, null, null);
	}

	public static String getChineseNumber(String number, String unit, String postfix) {

		String[] numbers = number.split(DEFAULT_PATH_SEPARATOR);
		if (numbers.length > 2) {
			new NumberFormatException("数字格式错误!");
		}
		int length = numbers[0].length();
		int isZero = 0;
		StringBuffer result = new StringBuffer();

		for (int i = 0; i < length; i++) {
			String digit = String.valueOf(numbers[0].charAt(i));

			boolean allZero = true; // 如果后继的全部是零,则跳出
			for (int j = i; j < length; j++) {
				if (numbers[0].charAt(j) != '0') {
					allZero = false;
					break;
				}
			}

			if (allZero) {
				boolean hasValue = false;
				for (int z = i; z >= 0; z--) {
					if (numbers[0].charAt(z) != '0' && length - z <= 7 && length - z >= 5) {
						hasValue = true;
						break;
					}
				}
				// 加万单位
				if ((length - i > 4 && length <= 8) || (hasValue && length - i > 4)) {
					result.append(BEFORE_SCALE[8]);
				}
				// 加亿单位
				if (length - i >= 9) {
					result.append(BEFORE_SCALE[4]);
				}
				break;
			}

			if (length < 9 && length - i == 5) {
				if (!"0".equals(digit) && isZero > 0) {
					result.append(NUMBER_MAPPING.get("0"));
				}
				if ("0".equals(digit)) {
					result.append(BEFORE_SCALE[8]);
					if (isZero > 0) {
						result.append(NUMBER_MAPPING.get("0"));
					}
					continue;
				}
			}
			if ("0".equals(digit) && length > 9 && length - i == 9) {
				result.append(BEFORE_SCALE[4]);
				continue;
			}

			if (isZero < 1 || !"0".equals(digit)) {
				if ("0".equals(digit)) {
					if (length - i != 6 && length - i != 7) {
						result.append(NUMBER_MAPPING.get(digit));
					}
				} else {
					result.append(NUMBER_MAPPING.get(digit));
				}

				if (!"0".equals(digit)) {
					result.append(BEFORE_SCALE[BEFORE_SCALE.length - length + i]);
				}
			}

			if ("0".equals(digit)) {
				isZero++;
			} else {
				isZero = 0;
			}
		}
		result.append(unit == null ? "圆" : result.append(unit));

		if (numbers.length == 1) {
			result.append(postfix == null ? "整" : result.append(postfix));
			return result.toString();
		}

		length = numbers[1].length();
		for (int j = 0; j < length; j++) {
			if (j > 2) {
				break;
			}
			if (numbers[1].charAt(j) == '0') {
				continue;
			}
			result.append(NUMBER_MAPPING.get(String.valueOf(numbers[1].charAt(j))));
			result.append(AFTER_SCALE[j]);
		}

		result.append(postfix == null ? "整" : result.append(postfix));

		return result.toString();
	}

	public static String getChineseNumber(Integer number) {
		return getChineseNumber(number.toString(), null, null);
	}

	public static String getChineseNumber(Double number) {
		DecimalFormat f = (DecimalFormat) DecimalFormat.getInstance();
		f.applyLocalizedPattern("#.##");
		return getChineseNumber(f.format(number), null, null);
	}

	/**
	 * 支持到12位
	 *
	 * @param numberStr 中文数字
	 * @return int 数字
	 */
	public static int getIntegerByNumberStr(String numberStr) {

		// 返回结果
		int sum = 0;

		// null或空串直接返回
		if (numberStr == null || ("").equals(numberStr)) {
			return sum;
		}

		// 过亿的数字处理
		if (numberStr.indexOf("亿") > 0) {
			String currentNumberStr = numberStr.substring(0, numberStr.indexOf("亿"));
			int currentNumber = testA(currentNumberStr);
			sum += currentNumber * Math.pow(10, 8);
			numberStr = numberStr.substring(numberStr.indexOf("亿") + 1);
		}

		// 过万的数字处理
		if (numberStr.indexOf("万") > 0) {
			String currentNumberStr = numberStr.substring(0, numberStr.indexOf("万"));
			int currentNumber = testA(currentNumberStr);
			sum += currentNumber * Math.pow(10, 4);
			numberStr = numberStr.substring(numberStr.indexOf("万") + 1);
		}

		// 小于万的数字处理
		if (!("").equals(numberStr)) {
			int currentNumber = testA(numberStr);
			sum += currentNumber;
		}

		return sum;
	}

	/**
	 * 把亿、万分开每4位一个单元，解析并获取到数据
	 * @param testNumber
	 * @return
	 */
	private static int testA(String testNumber) {
		// 返回结果
		int sum = 0;

		// null或空串直接返回
		if (testNumber == null || ("").equals(testNumber)) {
			return sum;
		}

		// 获取到千位数
		if (testNumber.indexOf("千") > 0) {
			String currentNumberStr = testNumber.substring(0, testNumber.indexOf("千"));
			sum += testB(currentNumberStr) * Math.pow(10, 3);
			testNumber = testNumber.substring(testNumber.indexOf("千") + 1);
		}

		// 获取到百位数
		if (testNumber.indexOf("百") > 0) {
			String currentNumberStr = testNumber.substring(0, testNumber.indexOf("百"));
			sum += testB(currentNumberStr) * Math.pow(10, 2);
			testNumber = testNumber.substring(testNumber.indexOf("百") + 1);
		}

		// 对于特殊情况处理 比如10-19是个数字，十五转化为一十五，然后再进行处理
		if (testNumber.indexOf("十") == 0) {
			testNumber = "一" + testNumber;
		}

		// 获取到十位数
		if (testNumber.indexOf("十") > 0) {
			String currentNumberStr = testNumber.substring(0, testNumber.indexOf("十"));
			sum += testB(currentNumberStr) * Math.pow(10, 1);
			testNumber = testNumber.substring(testNumber.indexOf("十") + 1);
		}

		// 获取到个位数
		if (!("").equals(testNumber)) {
			sum += testB(testNumber.replaceAll("零", ""));
		}

		return sum;
	}

	private static int testB(String replaceNumber) {
		switch (replaceNumber) {
		case "一":
			return 1;
		case "二":
			return 2;
		case "三":
			return 3;
		case "四":
			return 4;
		case "五":
			return 5;
		case "六":
			return 6;
		case "七":
			return 7;
		case "八":
			return 8;
		case "九":
			return 9;
		case "零":
			return 0;
		default:
			return 0;
		}
	}

	public static void main(String[] args) {
		System.out.println(getChineseNumber(1994));
		System.out.println(getChineseNumber(1994.1115));
		System.out.println(getChineseNumber(19941115));

		System.out.println(getIntegerByNumberStr("一万零一"));
	}

}